<template>
	<view class="container">
		<!--header-->
		<view class="tui-header-box" :style="{height:height+'px',background:'rgba(255,255,255,'+opcity+')'}">
			<view class="tui-header" :style="{paddingTop:top+'px', opacity:opcity}">
				房源详情
			</view>
			<view class="tui-header-icon" :style="{marginTop:top+'px'}">
				<view class="tui-icon tui-icon-arrowleft tui-icon-back" :style="{color:opcity>=1?'#000':'#fff',background:'rgba(0, 0, 0,'+iconOpcity+')'}"
				 @tap="back"></view>
			</view>
		</view>
		<!--header-->
		<!-- 轮播 -->
		<uni-swiper-dot :info="swiperList" :current="current" :mode="mode" :dots-styles="dotsStyles" field="content" v-if="loadShow">
			<swiper class="swiper-box" @change="change" :autoplay="autoplay" :circular="circular" :interval="interval" :current="current">
				<swiper-item v-for="(item ,index) in swiperList" :key="index">
					<view class="swiper-item" v-if="isVideo&&index == 0">
						<video :src="item" controls :poster="videoPic" objectFit="fill" @play="videoPlay" id="houseVideo"></video>
					</view>
					<view :class="item.colorClass" class="swiper-item" v-if="(isVideo&&index !=0) || (!isVideo) ">
						<image :src="item" mode="aspectFill" />
					</view>
				</swiper-item>
			</swiper>
			<view class="dotsList">
				<view class="dots" v-if="isVideo" @click="goVideo">
					<view class="dots-video" :class="[current == 0 ? 'active':'']">
						<image src="../../static/icon/TriangleCopy.png" mode=""></image>
						<text>视频</text>
					</view>
				</view>
				<view class="dots" v-if="isImg" @click="goImg">
					<view class="dots-img" :class="[current > 0&& current < length-1 ? 'active':'' ]" v-if="isVideo&&islayOut">
						<text>图片</text>
					</view>
					<view class="dots-img" :class="[current > 0&& current <= length-1 ? 'active':'' ]" v-if="isVideo&&!islayOut">
						<text>图片</text>
					</view>
					<view class="dots-img" :class="[current >= 0&& current < length-1 ? 'active':'' ]" v-if="!isVideo&&islayOut">
						<text>图片</text>
					</view>
					<view class="dots-img" :class="[current >= 0&& current <= length-1 ? 'active':'' ]" v-if="!isVideo&&!islayOut">
						<text>图片</text>
					</view>
				</view>
				<view class="dots" v-if="islayOut" @click="goLayOut">
					<view class="dots-img" :class="[current == length-1 ? 'active':'']">
						<text>户型</text>
					</view>
				</view>
			</view>
		</uni-swiper-dot>
		<view class="newHouse" v-if="loadShow">
			<view class="newHosueName">
				<view>
					<text>{{houseInfo.areaName}}</text>
				</view>
				<view>
					<view class="home-tips">
						<view :class="[{ 'tips-1': labIndex == 0, 'tips-2': labIndex != 0}]" v-for="(it, labIndex) in houseInfo.label" :key="labIndex">
							<text>{{ it }}</text>
						</view>
					</view>
					<view>
						<image src="../../static/icon/9.png" mode="" v-if="isCollect == 0" @click="collect"></image>
						<image src="../../static/icon/10.png" mode="" v-if="isCollect == 1" @click="noCollect"></image>
					</view>
				</view>
			</view>
			<view class="newHouseInfo">
				<view class="detail-item">
					<view>
						<text>单价</text><text style="color:#FE9E2E;">{{houseInfo.price}}元/m²</text>					
					</view>
				</view>
				<view class="detail-item">
					<view>
						<text>开盘</text><text>{{houseInfo.publish}}开盘</text>
					</view>
				</view>
				<view class="detail-item">
					<view>
						<text>地址</text><text style="color:#3270BD;font-weight: 100;">{{houseInfo.provinceString}}</text>
					</view>
				</view>
				<view class="checkMore" @click="checkMoreInfo">
					<text>查看更多信息</text>
				</view>
			</view>
		</view>
		<!-- 主力户型 -->
		<view class="houseTypeList" v-if="loadShow">
			<view>
				主力户型
			</view>
			<view>
				<scroll-view scroll-x="true" scroll-left="120" class="housTypeInfo">
					<view v-for="(item,index) in houseTypeList" :key="index" class="houseTypeDetail" @click="checkApartment(item.houseId)">
						<view>
							<image :src="item.picUrl" mode=""></image>
						</view>
						<view>
							<text>{{item.houseName}}</text>
							<text>{{item.acreage}}m²</text>
						</view>
						<view>
							<text v-if="item.price">约{{item.price}}万</text>
							<text v-else>暂无售价</text>
						</view>
						<view :class="[{'green':item.status == 1,'orange':item.status == 2,'gray':item.status == 3}]">
							<text v-if="item.status == 1">在售</text>
							<text v-if="item.status == 2">预售</text>
							<text v-if="item.status == 3">停售</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<!-- 经纪人信息 -->
		<view class="agent" @click="checkAgent(userId)" v-if="loadShow">
			<view class="agent-avatar">
				<image :src="avatar" mode=""></image>
			</view>
			<view class="agent-info">
				<text>{{userName}}</text>
				<text>{{company}}</text>
			</view>
			<view class="arrow-right">
				<image src="../../static/icon/DisclosureIndicator.png" mode=""></image>
			</view>
		</view>
		<!-- 猜你喜欢 -->
		<Guess :houseList="homeList" :houseType="houseType" v-if="loadShow"></Guess>
		<view class="bottom" v-if="loadShow">
			<view class="bottom-agent" @click="checkAgent(userId)">
				<view>
					<image :src="avatar" mode=""></image>
				</view>
				<view>
					<text>{{userName}}</text>
					<text>{{postName}}</text>
				</view>
			</view>
			<view class="bottom-share" @click="goShare">
				<image src="../../static/icon/share.png" mode=""></image>
				<text>分享</text>
			</view>
			<view class="bottom-call" @click="callTel">
				<image src="../../static/icon/dianhua-2.png" mode=""></image>
				<text>电话</text>
			</view>
		</view>
	</view>
</template>

<script>
	import uniSwiperDot from '@/components/uni-swiper-dot/uni-swiper-dot.vue'
	import baseURL from '../../api/index.js'	
	import Guess from '../../components/Guess/Guess.vue'
	import uniGrid from '../../components/uni-grid/uni-grid.vue'	
	export default {
		name:'newHouse',
		components:{
			uniSwiperDot,Guess,uniGrid
		},
		data(){
			return{
				houseId:'',
				height: 64, //header高度
				top: 0, //标题图标距离顶部距离
				scrollH: 0, //滚动总高度
				opcity: 0,
				iconOpcity: 0.5,
				current: 0,
				mode: 'nav',
				dotsStyles: {
						backgroundColor: 'rgba(0, 0, 0, .3)',
						border: '1px rgba(0, 0, 0, .3) solid',
						color: '#fff',
						selectedBackgroundColor: 'rgba(0, 0, 0, .9)',
						selectedBorder: '1px rgba(0, 0, 0, .9) solid'
				},
				autoplay:false,
				interval:3000,
				circular:true,
				swiperList:[],
				isVideo:'',//判断是否有视频
				videoPic:'',//视频封面图
				isImg:'',//是否有图片
				islayOut:'',//是否有户型图
				length:'',
				houseType:'',
				houseInfo:[],
				isCollect:0,
				houseTypeList:[],
				mobile:'',
				postName:'',
				userId:'',
				avatar:'',
				userName:'',
				company:'',
				homeList:[],
				isToken:false,
				manager:'',
				loadShow:false
			}
		},
		methods:{
			// 返回
			back(){
				uni.navigateBack({
					delta:1
				})
			},
			change(e) {
				this.current = e.detail.current
			},
			// 获取新房详情
			async getHouseInfo(){
				let res = await this.$fetch(this.$api.areaInfo,{areaId:this.houseId},'GET')
				this.isCollect = res.data.collect
				if(res.data.newArea.movieUrl){
					this.videoPic = res.data.newArea.movieFirst;
					this.swiperList.push(res.data.newArea.movieUrl)
					this.isVideo = true
				}else{
					this.isVideo = false
				}
				if(res.data.newArea.picUrl){
					let data = res.data.newArea.picUrl.split(',')
					data.forEach(item =>{
						this.swiperList.push(item)
					})
					this.isImg = true
				}else{
					this.isImg = false
				}
				if(res.data.newArea.layoutPic){
					let data = res.data.newArea.layoutPic.split(',')
					data.forEach(item =>{
						this.swiperList.push(item)
					})
					this.islayOut = true
				}else{
					this.islayOut = false
				}
				this.length = this.swiperList.length;
				if(!this.length){
					this.swiperList[0] = 'https://img.bajiaostar.com/FnwyfqiIK3_RD5UR1k66KQ_y0VFx'
				}
				this.houseInfo = res.data.newArea
				if(this.houseInfo.label){
					this.houseInfo.label = this.houseInfo.label.split(',')
				}
				this.houseInfo.publish = this.$dayjs(this.houseInfo.publish).format('YYYY年MM月')
				res.data.newHouseList.forEach(item =>{
					if(!item.status){
						item.status = '3'
					}
				})
				this.houseTypeList = res.data.newHouseList
				let manager = res.data.manager
				this.avatar = baseURL.pro+'profile/avatar/'+manager.avatar
				this.userName = manager.userName
				this.company = manager.company
				this.mobile = manager.phonenumber
				this.postName = manager.postName
				this.userId = manager.userId
				res.data.guess.forEach(item =>{
					if (item.picUrl) {
						item.picUrl = item.picUrl.split(',');
					}else{
						item.picUrl = 'https://img.bajiaostar.com/FnwyfqiIK3_RD5UR1k66KQ_y0VFx'.split(',')
					}
					if (item.label) {
						item.label = item.label.split(',');
					}
				})
				this.homeList = res.data.guess
				this.loadShow = true
				uni.hideLoading()
			},
			// 收藏
			async collect(){
				let res = await this.$fetch(this.$api.collect,{houseId:this.houseId,houseType:this.houseType},'GET')
				if(res.code == 0){
					this.isCollect = 1
				}
				uni.showToast({
					icon:'none',
					title:res.msg
				})
			},
			// 取消收藏
			async noCollect(){
				let res = await this.$fetch(this.$api.noCollect,{houseId:this.houseId,houseType:this.houseType},'GET')
				if(res.code == 0){
					this.isCollect = 0
				}
				uni.showToast({
					icon:'none',
					title:res.msg
				})
			},
			// 点击滑动到视频
			goVideo(){
				this.current = 0;
			},
			// 点击滑动到图片
			goImg(){
				if(this.isVideo){
					this.current = 1
				}else{
					this.current = 0
				}
			},
			// 点击滑动到户型
			goLayOut(){
				this.current = this.length-1
			},
			// 当视频播放后自动全屏
			videoPlay(){
				var videoContext = uni.createVideoContext('houseVideo')
				videoContext.requestFullScreen({direction:90})
			},
			// 拨打电话
			callTel(){
				uni.makePhoneCall({
					phoneNumber:this.mobile
				})
			},
			// 查看更多楼盘信息
			checkMoreInfo(){
				uni.navigateTo({
					url:'moreInfo?houseId='+this.houseId+'&houseType=3'
				})
			},
			// 查看户型详情
			checkApartment(id){
				uni.navigateTo({
					url:'apartment?id='+id
				})
			},
			// 查看经纪人详情
			checkAgent(id){
				uni.navigateTo({
					url:'../agent/agentDetail?id='+id
				})
			},
			// 分享
			goShare(){
				if(this.isToken){
					uni.navigateTo({
						url:'../share/newShare?id='+this.houseId+'&houseType='+this.houseType
					})
				}else{
					uni.showToast({
						icon:'none',
						title:'请先登录！'
					})
					setTimeout(()=>{
						uni.navigateTo({
							url:'../login/login'
						})
					},1000)
				}
			}
		},
		onLoad(options) {
			uni.showLoading({
				title:'加载中'
			})
			let obj = {};
			// #ifdef MP-WEIXIN
			obj = wx.getMenuButtonBoundingClientRect();
			// #endif
			// #ifdef MP-BAIDU
			obj = swan.getMenuButtonBoundingClientRect();
			// #endif
			// #ifdef MP-ALIPAY
			my.hideAddToDesktopMenu();
			// #endif
			uni.getSystemInfo({
				success: (res) => {
					this.width = obj.left || res.windowWidth;
					this.height = obj.top ? (obj.top + obj.height + 8) : (res.statusBarHeight + 44);
					this.top = obj.top ? (obj.top + (obj.height - 32) / 2) : (res.statusBarHeight + 6);
					this.scrollH = res.windowWidth
				}
			})
			if(options.scene){
				let scene = decodeURIComponent(options.scene)
				this.houseId = scene.split('&')[0]
				this.houseType = scene.split('&')[1]
				if(scene.split('&')[2]){
					this.manager = scene.split('&')[2]
					uni.setStorageSync('manager',this.manager)
				}
			}else{
				this.houseId = options.houseId
				this.houseType = options.houseType
				if(options.userId){
					this.manager = options.userId
					uni.setStorageSync('manager',this.manager)
				}
			}
			this.getHouseInfo()
		},
		onShow() {
			if(uni.getStorageSync('isToken')){
				this.isToken = uni.getStorageSync('isToken')
			}
		},
		onPageScroll(e) {
			let scroll = e.scrollTop <= 0 ? 0 : e.scrollTop;
			let opcity = scroll / this.scrollH;
			if (this.opcity >= 1 && opcity >= 1) {
				return;
			}
			this.opcity = opcity;
			this.iconOpcity = 0.5 * (1 - opcity < 0 ? 0 : 1 - opcity)
		}
	}
</script>

<style lang="less">
	@import "../../static/style/icon.css";
	page{
		background: #F0F0F0;
	}
	.container {
		padding-bottom: 110upx;
	}
	.tui-header-box {
		width: 100%;
		position: fixed;
		left: 0;
		top: 0;
		z-index: 10;
	}
	
	.tui-header {
		width: 100%;
		font-size: 18px;
		line-height: 18px;
		font-weight: 500;
		height: 32px;
		display: flex;
		align-items: center;
		justify-content: center;
	}
	
	.tui-header-icon {
		position: fixed;
		top: 0;
		left: 10px;
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
		height: 32px;
		transform: translateZ(0);
		z-index: 11;
	}
	
	
	
	.tui-header-icon .tui-badge {
		background: #e41f19 !important;
		position: absolute;
		right: -4px;
	}
	
	.tui-icon-ml {
		margin-left: 20upx;
	}
	
	.tui-icon {
		border-radius: 16px;
	}
	
	
	.tui-icon-back {
		height: 32px !important;
		width: 32px !important;
		display: block !important;
	}
	
	.tui-header-icon .tui-icon-more-fill {
		height: 20px !important;
		width: 20px !important;
		padding: 6px !important;
		display: block !important;
	}
	swiper{
		width: 750upx;
		height: 540upx;
	}
	.swiper-box {
		height: 540upx;
	}
	.swiper-item {
		display: flex;
		justify-content: center;
		align-items: center;
		height: 100%;
		background: #eee;
		color: #fff;
		background-repeat: no-repeat;
		background-size: 100% 100%;
		background-image: url('https://img.bajiaostar.com/FnwyfqiIK3_RD5UR1k66KQ_y0VFx');
	}
	
	.swiper-item image {
		width: 100%;
		height: 100%;
	}
	.swiper-item video{
		width: 100%;
		height: 100%;
	}
	.dotsList{
		width:340rpx ;
		height: 40rpx;
		position: absolute;
		bottom: 30rpx;
		left: 206rpx;
		display: flex;
	}
	.dots{
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		height: 40rpx;
	}
	.dots-video{
		width: 100rpx;
		height: 40rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background: rgba(255,255,255,0.70);
		border-radius: 20rpx;
		font-size: 24rpx;
		color: #5B5B5B;
	}
	.dots-video image{
		width: 16rpx;
		height: 16rpx;
		margin-right: 5rpx;
	}
	.dots-img{
		width: 90rpx;
		height: 40rpx;
		text-align: center;
		line-height: 40rpx;
		background: rgba(255,255,255,0.70);
		border-radius: 20rpx;
		font-size: 24rpx;
		color: #5B5B5B;
	}
	.active{
		background: #874A98;
		color: #FFFFFF;
	}
	.newHouse{
		width: 100%;
		display: flex;
		flex-direction: column;
		background: #FFFFFF;
	}
	.newHosueName{
		width: 690rpx;
		margin: 0 30rpx;
		border-bottom: 2rpx solid #F0F0F0;
		
		& > view:first-child{
			margin-top: 30rpx;
			width: 690rpx;
			font-size: 36rpx;
			color: #5B5B5B;
			font-weight: bold;
		}
		
		& > view:last-child{
			margin:18rpx 0 30rpx 0;
			display: flex;
			width: 690rpx;
			
			.home-tips{
				width: 648rpx;
				display: flex;
				align-items: center;
				
				.tips-1{
					background: #48D2AB;
					border-radius: 8rpx;
					font-size: 20rpx;
					color: #FFFFFF;
					letter-spacing: 0;
					text-align: justify;
					margin-right: 30rpx;
					height: 36rpx;
					line-height: 36rpx;
					padding: 0 10rpx;
				}
				
				.tips-2{
					background: rgba(74,144,226,0.15);
					border-radius: 8rpx;
					font-size: 20rpx;
					color: #4A90E2;
					letter-spacing: 0;
					text-align: justify;
					margin-right: 30rpx;
					height: 36rpx;
					line-height: 36rpx;
					padding: 0 10rpx;
				}
			}
			
			& > view:last-child{
				width: 42rpx;
				display: flex;
				align-items: center;
				
				image{
					width: 42rpx;
					height: 37.8rpx;
				}
			}
		}
	}
	.newHouseInfo{
		width: 690rpx;
		display: flex;
		flex-direction: column;
		margin: 34rpx 30rpx 0 30rpx;
	}
	.detail-item{
		width: 690rpx;
		height: 40rpx;
		display: flex;
		margin-bottom: 20rpx;
		
		view{
			flex: 1;
			display: flex;
			
			& > text:first-child{
				font-size: 28rpx;
				color: #A6A6A6;
				margin-right: 20rpx;
				white-space: nowrap;
			}
			
			& > text:nth-child(2){
				font-size: 28rpx;
				color: #5B5B5B;
				white-space: nowrap;
				text-overflow: ellipsis;
				overflow: hidden;
				font-weight: 600;
			}
		}
	}
	.checkMore{
		width: 690rpx;
		height: 70rpx;
		font-size: 24rpx;
		text-align: center;
		line-height: 70rpx;
		color: #3270BD;
		background: #F0F1F1;
		border-radius: 4rpx;
		margin-bottom: 30rpx;
	}
	.houseTypeList{
		margin-top: 30rpx;
		width: 100%;
		background: #FFFFFF;
		display: flex;
		flex-direction: column;
		
		& > view:first-child{
			width: 690rpx;
			margin-left: 30rpx;
			height: 90rpx;
			line-height: 90rpx;
			font-size: 28rpx;
			color: #A6A6A6;
			border-bottom: 2rpx solid #F0F0F0;
		}
		
		& > view:last-child{
			width: 100%;
			white-space: nowrap;
		}
	}
	.housTypeInfo{
		display: flex;
		flex-direction: row;
		
		& > view:last-child{
			margin-right: 30rpx;
		}
		
		.houseTypeDetail{
			width:240rpx;
			margin: 30rpx 0 30rpx 30rpx;
			display: inline-block;
			
			& > view:first-child{
				width: 240rpx;
				height: 240rpx;
				border: 2rpx solid #F0F1F1;
				
				image{
					width: 240rpx;
					height: 240rpx;
				}
			}
			
			& > view:nth-child(2){
				margin-top: 20rpx;
				display: flex;
				font-size: 28rpx;
				color: #5B5B5B;
				
				& > text:first-child{
					flex: 1;
				}
				
				& > text:last-child{
					flex: 1;
					text-align: right;
				}
			}
			
			& > view:nth-child(3){
				margin-top: 10rpx;
				font-size: 28rpx;
				color: #FE9E2E;
				font-weight: bold;
			}
			
			& > view:last-child{
				margin-top: 10rpx;
				width: 60rpx;
				height: 36rpx;
				font-size: 20rpx;
				line-height: 36rpx;
				padding: 0 10rpx;
				border-radius: 4rpx;
				text-align: center;
			}
			
			.orange{
				background: #F2845C;
				color: #FFFFFF;
			}
			
			.green{
				background: #48D2AB;
				color: #FFFFFF;
			}
			
			.gray{
				background: #F0F1F1;
				color: #A6A6A6;
			}
		}
	}
	.agent{
		width: 100%;
		height: 148rpx;
		background: #FFFFFF;
		margin-top: 30rpx;
		display: flex;
		align-items: center;
	}
	.agent-avatar{
		margin: 0 30rpx;
		width: 90rpx;
		height: 90rpx;
		border-radius: 50%;
		background-repeat: no-repeat;
		background-size: 100% 100%;
		background-image: url('https://img.bajiaostar.com/FgkVAFlA5etvApeTeLEt2P0s822H');
		
		& > image{
			width: 90rpx;
			height: 90rpx;
			border-radius: 50%;
		}
	}
	.agent-info{
		width: 554rpx;
		height: 148rpx;
		display: flex;
		flex-direction: column;
		
		& > text:first-child{
			margin-top: 30rpx;
			font-size: 28rpx;
			color: #5B5B5B;
			font-weight: bold;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
		}
		
		& > text:nth-child(2){
			font-size: 24rpx;
			margin-top: 10rpx;
			color: #5B5B5B;
			white-space: nowrap;
			text-overflow: ellipsis;
			overflow: hidden;
		}
	}
	.arrow-right{
		width: 16rpx;
		height: 148rpx;
		display: flex;
		align-items: center;
		
		& > image{
			width: 16rpx;
			height: 26rpx;
		}
	}
	.bottom{
		width: 100%;
		background: #FFFFFF;
		height: 100rpx;
		display: flex;
		position: fixed;
		bottom: 0;
		z-index: 10;
	}
	.bottom-agent{
		width: 270rpx;
		height: 100rpx;
		display: flex;
		align-items: center;
		
		& > view:first-child{
			margin: 0 20rpx 0 30rpx;
			width: 70rpx;
			height: 70rpx;
			border-radius: 50%;
			background-repeat: no-repeat;
			background-size: 100% 100%;
			background-image: url('https://img.bajiaostar.com/FgkVAFlA5etvApeTeLEt2P0s822H');
			
			image{
				width: 70rpx;
				height: 70rpx;
				border-radius: 50%;
			}
		}
		
		& > view:nth-child(2){
			height: 100rpx;
			display: flex;
			flex-direction: column;
			
			& > text:first-child{
				margin-top: 12rpx;
				font-size: 28rpx;
				color: #5B5B5B;
				font-weight: bold;
			}
			
			& > text:nth-child(2){
				margin-top: 10rpx;
				font-size: 24rpx;
				color: #A6A6A6;
			}
		}
	}
	.bottom-share{
		width: 240rpx;
		height: 100rpx;
		background-image: linear-gradient(45deg, #FFBF2F 0%, #FF9417 100%);
		display: flex;
		align-items: center;
		justify-content: center;
		
		& > image{
			width: 36rpx;
			height: 36rpx;
		}
		
		& > text{
			margin-left: 10rpx;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}
	.bottom-call{
		width: 240rpx;
		height: 100rpx;
		background-image: linear-gradient(45deg, #AC63C0 0%, #874A98 100%);
		display: flex;
		align-items: center;
		justify-content: center;
		
		& > image{
			width: 36rpx;
			height: 36rpx;
		}
		
		& > text{
			margin-left: 10rpx;
			font-size: 28rpx;
			color: #FFFFFF;
		}
	}
</style>
